<template>
	<view class="website">
		<view class="tabs">
			<view class="tabs-item" :class="{ active: form.type === 1 }" @tap="changeTab(1)">微官网</view>
			<view class="tabs-item" :class="{ active: form.type === 2 }" @tap="changeTab(2)">URL链接</view>
		</view>
		<view class="section">
			<view v-if="form.type === 1">
				<!-- <template v-if="form.ximg">
					<view class="img">
						<image :src="form.ximg" mode="aspectFit"></image>
						<icon type="clear" size="20" @tap="remove('ximg')"/>
					</view>
				</template>
<template v-else>
					<view class="add-section" @tap="chooseImg('ximg')">
						<view>
							<text>点击上传宣传图片</text>
							<view class="btn">编辑</view>
						</view>					
					</view>	
				</template> -->
				<div class="title">宣传图片</div>
				<view class="img-box">
					<view v-for="(item, index) in form.ximg" key="index" class="img">
						<image :src="item" mode=""></image>
						<icon v-if="userInfo.userType" type="clear" size="20" @tap="remove2(index, 'ximg')" />
					</view>
					<view v-if="userInfo.userType && form.ximg.length < 5" class="add-btn" @tap="chooseImg('ximg')">上传图片</view>
				</view>
				<!-- <template v-if="form.qimg">
					<view class="img">
						<image :src="form.qimg" mode="aspectFit"></image>
						<icon type="clear" size="20" @tap="remove('qimg')"/>
					</view>
				</template>
				<template v-else>
					<view class="add-section" @tap="chooseImg('qimg')">
						<view>
							<text>点击上传企业图片</text>
							<view class="btn">编辑</view>
						</view>
					</view>
				</template>	 -->
				<div class="title">企业图片</div>
				<view class="img-box">
					<view v-for="(item, index) in form.qimg" key="index" class="img">
						<image :src="item" mode=""></image>
						<icon v-if="userInfo.userType" type="clear" size="20" @tap="remove2(index, 'qimg')" />
					</view>
					<view v-if="userInfo.userType && form.qimg.length < 5" class="add-btn" @tap="chooseImg('qimg')">上传图片</view>
				</view>
				<view class="textarea-box">
					<!-- <view>
						<text>点击编辑企业介绍</text>
						<view class="btn">编辑</view>
					</view> -->
					<textarea v-model="form.companyIntroduction" :disabled="!userInfo.userType" cols="30" rows="10" placeholder="请输入企业介绍内容"></textarea>
				</view>
			</view>
			<view v-else>
				<view class="input-box">
					<input v-model="form.websiteUrl" :disabled="true" type="text" placeholder="请输入网站地址">
				</view>
			</view>
		</view>
		<view v-if="userInfo.userType" class="btn-box">
			<view class="btn" @tap.stop="submit">保存</view>
		</view>
		<loginModal ref="loginModal" @success="success" />
		<CustomTabBar :selected="3" :display="display"/>
	</view>
</template>

<script>
import http from '@/utils/http.js'
import loginModal from '@/components/login/index.vue'
import CustomTabBar from '@/components/CustomTabBar/index.vue'
export default {
	components: { loginModal, CustomTabBar },
	data() {
		return {
			token: '',
			form: {
				type: 1,
				qimg: [],
				ximg: [],
				websiteUrl: '',
				companyIntroduction: ''
			},
			userInfo: {},
			display: 0
		}
	},
	onLoad() {
		// this.getData()
		this.token = uni.getStorageSync('token')
		if (this.token) {
			this.getData()
		}
	},
	onShow() {
		this.getUserData()
	},
	methods: {
		success() {
			this.token = uni.getStorageSync('token')
			if (this.token) {
				this.getData()
			}
		},
		login() {
			// uni.navigateTo({
			// 	url: '/pages/login/index'
			// })
			this.$refs.loginModal.open()
		},
		remove2(index, key) {
			this.form[key].splice(index, 1)
		},
		confirm(e) {

		},
		changeTab(n) {
			this.form.type = n
		},
		goEdit() {
			uni.navigateTo({
				url: '/pages/module/introduce'
			})
		},
		remove(key) {
			this.form[key] = ''
		},
		upload(filePath, key) {
			debugger
			const fileName = filePath.split('/').pop()
			uni.uploadFile({
				url: `https://ywtmp.hnwytkj.com/app-api/infra/file/upload?path=${fileName}`,
				filePath,
				name: 'file',
				// formData: {},
				success: (uploadFileRes) => {
					const res = JSON.parse(uploadFileRes.data)
					console.log(uploadFileRes.data);
					if (res.code === 0) {
						this.form[key].push(res.data)
					}
				},
				fail: (res) => {
					console.log(res)
				}
			});
		},
		chooseImg(key) {
			if (!this.token) {
				uni.showModal({
					title: '提示',
					content: '您还未登录，去登录？',
					success: (res) => {
						if (res.confirm) {
							// uni.navigateTo({
							// 	url: '/pages/login/index'
							// })
							this.login()
						}
					}
				});
				return false
			}
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album'], //从相册选择
				success: (res) => {
					console.log(res.tempFilePaths)
					this.upload(res.tempFilePaths[0], key)
				}
			});
		},
		submit() {
			if (!this.token) {
				uni.showModal({
					title: '提示',
					content: '您还未登录，去登录？',
					success: (res) => {
						if (res.confirm) {
							// uni.navigateTo({
							// 	url: '/pages/login/index'
							// })
							this.login()
						}
					}
				});
				return false
			}
			if (this.form.type === 1) {
				if (!this.form.ximg.length) {
					uni.showToast({
						title: '请上传宣传图片',
						icon: "none"
					});
					return false
				}
				if (!this.form.qimg.length) {
					uni.showToast({
						title: '请上传企业图片',
						icon: "none"
					});
					return false
				}
				if (!this.form.companyIntroduction) {
					uni.showToast({
						title: '请输入企业介绍',
						icon: "none"
					});
					return false
				}
			} else {
				if (!this.form.websiteUrl) {
					uni.showToast({
						title: '请输入网址地址',
						icon: "none"
					});
					return false
				}					
			}
			http({
				url: 'card/micro-website/create',
				method: 'put',
				data: {
					...this.form,
					ximg: JSON.stringify(this.form.ximg),
					qimg: JSON.stringify(this.form.qimg),
					id: uni.getStorageSync('userId')
				}
			}).then(res => {
				if (res.code === 0) {
					uni.showToast({
						title: '保存成功',
						icon: "success"
					});
				}
			})
		},
		getData() {
			const userId = uni.getStorageSync('userId')
			http({
				url: `card/micro-website/get?id=${userId}`
			}).then(res => {
				if (res.code === 0) {
					try {
						this.form = {
							...res.data,
							type: res.data.type || 1,
							qimg: res.data.qimg ? JSON.parse(res.data.qimg) : [],
							ximg: res.data.ximg ? JSON.parse(res.data.ximg) : []
						}
					} catch (e) {
						//TODO handle the exception
						this.form = {
							...res.data,
							type: res.data.type || 1,
							qimg: [],
							ximg: []
						}
					}
				}
			})
		},
		getUserData() {
			const token = uni.getStorageSync('token');
			if (token) {
				http({
					url: 'card/user-profile/get'
				}).then(res => {
					if (res.code === 0) {
						this.display = res.data.display || 0
						this.userInfo = {
							...res.data.userProfileDO
						}
					}
				})
			}
		}
	},
}
</script>

<style lang="scss" scoped>
.website {
	min-height: 100vh;
	background-color: #F0F0F4;
	background-image: linear-gradient(180deg, #4B73FF, #F0F0F4);
	background-size: 100% 447rpx;
	background-repeat: no-repeat;
	padding: 30rpx;
	padding-bottom: calc(181rpx + var(--safe-area-inset-bottom));
	box-sizing: border-box;
	font-family: PingFang SC;

	.tabs {
		font-weight: bold;
		font-size: 34rpx;
		color: #FFFFFF;
		display: flex;
		padding: 0 24rpx;

		.tabs-item {
			position: relative;
			padding-bottom: 36rpx;
			margin-left: 102rpx;

			&:first-child {
				margin-left: 0;
			}

			&.active {
				&::after {
					content: '';
					width: 40rpx;
					height: 6rpx;
					background: #FFFFFF;
					border-radius: 3rpx;
					position: absolute;
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
				}
			}
		}
	}

	.section {
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0 0;
		margin-top: 20rpx;
		// min-height: calc(100vh - 320rpx - var(--safe-area-inset-bottom));
		// padding: 30rpx 30rpx calc(30rpx + var(--safe-area-inset-bottom));
		// min-height: calc(100vh - 320rpx);
		min-height: 900rpx;
		padding: 30rpx;
		box-sizing: border-box;
		// .add-section {
		// 	height: 261rpx;
		// 	background: #E6ECFF;
		// 	border-radius: 20rpx;
		// 	border: 1rpx dashed #4B73FF;
		// 	box-sizing: border-box;
		// 	margin-bottom: 20rpx;
		// 	display: flex;
		// 	align-items: center;
		// 	justify-content: center;
		// 	text-align: center;
		// 	text {
		// 		font-weight: 500;
		// 		font-size: 28rpx;
		// 		color: #4B73FF;
		// 	}
		// 	.btn {
		// 		width: 130rpx;
		// 		height: 70rpx;
		// 		line-height: 70rpx;
		// 		text-align: center;
		// 		background: #4B73FF;
		// 		border-radius: 30rpx;
		// 		font-weight: 500;
		// 		font-size: 26rpx;
		// 		color: #FFFFFF;
		// 		margin: 0 auto;
		// 		margin-top: 20rpx;
		// 	}				
		// }
		// .img {
		// 	height: 261rpx;
		// 	border-radius: 20rpx;
		// 	margin-bottom: 20rpx;
		// 	position: relative;
		// 	image {
		// 		width: 100%;
		// 		height: 261rpx;
		// 		border-radius: 20rpx;					
		// 	}
		// 	icon {
		// 		position: absolute;
		// 		top: -20rpx;
		// 		right: -20rpx;
		// 	}
		// }
		.title {
			font-size: 28rpx;
			margin-bottom: 20rpx;
		}

		.img-box {
			display: flex;
			align-items: center;
			flex-wrap: wrap;

			.img {
				width: 162rpx;
				height: 162rpx;
				border-radius: 20rpx;
				margin-right: 20rpx;
				margin-bottom: 40rpx;
				position: relative;

				image {
					width: 162rpx;
					height: 162rpx;
					border-radius: 20rpx;
				}

				icon {
					position: absolute;
					top: -20rpx;
					right: -20rpx;
				}
			}

			.add-btn {
				width: 162rpx;
				height: 162rpx;
				background: #E6ECFF;
				border-radius: 20rpx;
				border: 1rpx dashed #4B73FF;
				display: flex;
				align-items: center;
				justify-content: center;
				font-weight: 500;
				font-size: 28rpx;
				color: #4B73FF;
				margin-bottom: 40rpx;
			}
		}

		.textarea-box {
			textarea {
				font-weight: 500;
				font-size: 28rpx;
				// color: #CCCCCC;
				color: #333333;
				border-radius: 10rpx;
				border: 1rpx solid #EEEEEE;
				width: 100%;
				// height: 773rpx;
				height: 261rpx;
				box-sizing: border-box;
				padding: 28rpx 24rpx;

				&::placeholder {
					color: #CCCCCC;
				}
			}
		}

		.input-box {
			input {
				border-radius: 10rpx;
				border: 1rpx solid #EEEEEE;
				font-weight: 500;
				font-size: 28rpx;
				height: 100rpx;
				padding: 0 24rpx;
			}
		}
	}

	.btn-box {
		// position: fixed;
		// bottom: calc(143rpx + var(--safe-area-inset-bottom));
		// left: 0;
		// width: 100%;
		// z-index: 10;
		padding: 20rpx;
		box-sizing: border-box;
		background-color: #FFFFFF;		
		border-radius: 0 0 20rpx 20rpx;
		.btn {
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			background: #4B73FF;
			border-radius: 20rpx;
			font-weight: bold;
			font-size: 28rpx;
			color: #FFFFFF;
			// margin-top: 42rpx;
		}
	}
}
</style>